package com.anjiplus.template.gaea.business.modules.flowable.process.controller;

import com.anji.plus.gaea.annotation.Permission;
import com.anji.plus.gaea.bean.ResponseBean;
import com.anji.plus.gaea.curd.controller.GaeaBaseController;
import com.anji.plus.gaea.curd.service.GaeaBaseService;
import com.anjiplus.template.gaea.business.modules.flowable.process.controller.dto.ActReProcdefDTO;
import com.anjiplus.template.gaea.business.modules.flowable.process.controller.dto.TaskInfoDTO;
import com.anjiplus.template.gaea.business.modules.flowable.process.controller.param.ActReProcdefParam;
import com.anjiplus.template.gaea.business.modules.flowable.process.controller.param.RunningTaskParam;
import com.anjiplus.template.gaea.business.modules.flowable.process.dao.entity.ActReProcdef;
import com.anjiplus.template.gaea.business.modules.flowable.process.service.ProcessService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.flowable.ui.modeler.domain.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 流程控制類
 *
 * @author lirui
 * @since 2021-03-18 14:41:30
 */
@RestController
@RequestMapping("/process")
@Api(value = "/process", tags = "")
@Permission(code = "process",name = "")
public class ProcessController extends GaeaBaseController<ActReProcdefParam, ActReProcdef, ActReProcdefDTO> {

    @Autowired
    private ProcessService processService;

    @Override
    public GaeaBaseService<ActReProcdefParam, ActReProcdef> getService() {
        return processService;
    }

    @Override
    public ActReProcdef getEntity() {
        return new ActReProcdef();
    }

    @Override
    public ActReProcdefDTO getDTO() {
        return new ActReProcdefDTO();
    }

    /**
     * 启动流程
     * @param key
     * @param body
     * @return
     */
    @PostMapping("/start/process/{key}")
    public ResponseBean startProcess(@PathVariable("key") String key, @RequestBody Map<String,Object> body) {
        processService.startProcess(key, key, body);
        return responseSuccess();
    }

    /**
     * 待办任务
     * @return
     */
    @GetMapping("/my/running/task")
    public ResponseBean myRunningTask(RunningTaskParam param) {
        IPage<TaskInfoDTO> taskIPage = processService.getRunningTasks(param);

        return responseSuccessWithData(taskIPage);
    }

    /**
     * 已办任务
     * @return
     */
    @GetMapping("/my/history/task")
    public ResponseBean myHistoryTask(RunningTaskParam param) {
        IPage<TaskInfoDTO> taskIPage = processService.getHistoricTaskInstanceIPage(param);
        return responseSuccessWithData(taskIPage);
    }

    /**
     * 查询任务详情
     * @return
     */
    @GetMapping("/task/detail/{taskId}")
    public ResponseBean taskDetail(@PathVariable("taskId") String taskId) {
        return responseSuccessWithData(processService.taskDetail(taskId));
    }


    /**
     * 查询流程实例
     * @return
     */
    @GetMapping("/processInstance/detail/{processInstanceId}")
    public ResponseBean processInstanceDetail(@PathVariable("processInstanceId") String processInstanceId) {
        return responseSuccessWithData(processService.processInstanceDetail(processInstanceId));
    }

    /**
     * 完成任务
     * @param taskId
     * @param requestBody
     * @return
     */
    @PostMapping("/complete/task/{taskId}")
    public ResponseBean handlerTask(@PathVariable("taskId") String taskId, @RequestBody Map<String,Object> requestBody) {
        processService.completeTask(taskId, requestBody);
        return responseSuccess();
    }


    /**
     * 获取表单数据
     * @param processDefinitionId
     * @return
     */
    @GetMapping("/form/data/{processDefineId}")
    public ResponseBean getFormData(@PathVariable("processDefineId") String processDefinitionId) {

        Model modelForm = processService.getFormModel(processDefinitionId);
        return responseSuccessWithData(modelForm);
    }
}
